package com.itheima.edu.info.manager.system.service;

import com.itheima.edu.info.manager.system.dao.BaseStudentDao;
import com.itheima.edu.info.manager.system.dao.OtherStudentDao;
import com.itheima.edu.info.manager.system.dao.StudentDao;
import com.itheima.edu.info.manager.system.domain.Student;
import com.itheima.edu.info.manager.system.factroy.StudentFactory;

/**
 * 操作学生  业务类
 * 有一个东西他是专门操作数据的
 */
public class StudentService {
   //private StudentDao dao = new StudentDao();
   //private OtherStudentDao dao = new OtherStudentDao();
   private BaseStudentDao dao = StudentFactory.getStudentDao();
   /* public StudentDao getDao() {
        return dao;
    }

    public void setDao(StudentDao dao) {
        this.dao = dao;
    }*/

    /**
     * service 添加学生操作
     *
     * @param student
     * @return
     */
    public boolean addStudent(Student student) {
        //创建学生Dao 对象 调用Dao方法
        //StudentDao dao = new StudentDao();
        return dao.addStudent(student);

    }

    /**
     * 判断学号是否存在
     *
     * @param id
     */
    public boolean isExits(String id) {
        //查询所有的学生
      Student[] stus= dao.findAllStudents();
        //遍历学生数组 判断是否存在
        boolean flag= false;
        for (int i = 0; i < stus.length; i++) {
            //stus[i]
            Student s= stus[i];
            if(s!=null&&s.getId().equals(id)){
                flag=true;
                break;
                //return true;
            }
        }

        return flag;
        //存在 return true
        //不存在return false


    }

    /**
     * service 查询所有学生的方法
     * @return
     */
    public Student[] findAllStudents() {
        //1.dao 调用dao 查询所有学生的方法
        Student[] allStudents = dao.findAllStudents();

        for (int i = 0; i < allStudents.length; i++) {
            //allStudents[i]
            Student s= allStudents[i];
            // //3.不为空 才能返回数组
            if(s!=null){
                return allStudents;
            }
        }

        //2.判断数组是否为空
        return null;
    }

    /**
     * service  根据学号删除学生信息
     * @param id
     */
    public void deleteStudentById(String id) {

        dao.deleteById(id);
    }

    public void updateStudent(String id, Student student) {
        dao.updateStudent(id,student);

    }
}
